﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_website_gov_newsaddup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Common.CheckGovOnline();
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }

                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }
            }
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.DelGovNews(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.GetGovNews(int.Parse(hidId.Value));
                if (dt.Rows.Count > 0)
                {
                    this.txtType.Text = dt.Rows[0]["type"].ToString();
                    this.txtTitle.Text = dt.Rows[0]["title"].ToString();
                    this.editor_id.Value = dt.Rows[0]["content"].ToString();
                }
            }
        }

        protected void cmdOk_Click(object sender, EventArgs e)
        {
            string sType = this.txtType.Text.Trim();
            string sTitle = this.txtTitle.Text.Trim();
            string sContent = this.editor_id.Value.Trim();

            if (sTitle == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "标题不能为空！");
                return;
            }

            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.AddGovNews(int.Parse(Session["GovId"].ToString()), sType, sTitle, sContent))
                {
                    PM.Web.Common.JsAlert(this, "保存成功！",true);
                }
            }
            else
            {
                if (dal.ModifyGovNews(int.Parse(hidId.Value), sType, sTitle, sContent))
                {
                    PM.Web.Common.JsAlert(this, "修改成功！",true);
                }
            }
        }
    }
}